package com.ruoyi.net.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.*;
import com.ruoyi.net.mapper.*;
import com.ruoyi.net.service.IBusiL2VpnAlarmoamService;
import com.ruoyi.net.service.IBusiL2VpnService;
import com.ruoyi.net.utils.autoIncrement.GlobalIncrementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务-L2 VPN配置Service业务层处理
 * 
 * @author tony
 * @date 2024-12-14
 */
@Service
public class BusiL2VpnServiceImpl implements IBusiL2VpnService 
{
    @Autowired
    private GlobalIncrementService globalIncrementService;

    @Autowired
    private BusiL2VpnMapper busiL2VpnMapper;

    @Autowired
    private BusiL2VpnNodeMapper busiL2VpnNodeMapper;

    @Autowired
    private BusiL2VpnNodeInfoMapper busiL2VpnNodeInfoMapper;

    @Autowired
    private BusiL2VpnNodePortMapper busiL2VpnNodePortMapper;

    @Autowired
    private BusiL2VpnPwMapper busiL2VpnPwMapper;

    @Autowired
    private BusiL2VpnQoslspMapper busiL2VpnQoslspMapper;

    @Autowired
    private BusiL2VpnQosflowMapper busiL2VpnQosflowMapper;

    @Autowired
    private BusiL2VpnQoslevelMapper busiL2VpnQoslevelMapper;

    @Autowired
    private BusiElementStatusMapper busiElementStatusMapper;

    @Autowired
    private IBusiL2VpnAlarmoamService l2VpnAlarmoamService;

    /**
     * 查询业务-L2 VPN配置
     * 
     * @param id 业务-L2 VPN配置主键
     * @return 业务-L2 VPN配置
     */
    @Override
    public BusiL2Vpn selectBusiL2VpnById(String id)
    {
        return busiL2VpnMapper.selectBusiL2VpnById(id);
    }

    /**
     * 查询业务-L2 VPN配置列表
     * 
     * @param busiL2Vpn 业务-L2 VPN配置
     * @return 业务-L2 VPN配置
     */
    @Override
    public List<BusiL2Vpn> selectBusiL2VpnList(BusiL2Vpn busiL2Vpn)
    {
        return busiL2VpnMapper.selectBusiL2VpnList(busiL2Vpn);
    }

    @Override
    public List<BusiL2VpnVo> selectBusiL2VpnListVo(BusiL2VpnVo busiL2VpnVo)
    {
        return busiL2VpnMapper.selectBusiL2VpnListVo(busiL2VpnVo);
    }

    /**
     * 新增业务-L2 VPN配置
     * 
     * @param busiL2Vpn 业务-L2 VPN配置
     * @return 结果
     */
    @Override
    public int insertBusiL2Vpn(BusiL2Vpn busiL2Vpn) {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(busiL2Vpn.getId())) {
            busiL2Vpn.setId(uuid);
        }
        // 后端生成
        busiL2Vpn.setBusiName("vpws_" + globalIncrementService.getNextVPWSIncrementNumber());
        int i = 0;
        if (busiL2VpnMapper.selectBusiL2VpnById(busiL2Vpn.getId()) != null) {
            //更新  删除节点列表、pw列表
            busiL2VpnNodeMapper.deleteBusiL2VpnNodeByL2VpnId(busiL2Vpn.getId());
            busiL2VpnQoslspMapper.deleteBusiL2VpnQoslspByL2VpnId(busiL2Vpn.getId());
            busiL2VpnQosflowMapper.deleteBusiL2VpnQosflowByL2VpnId(busiL2Vpn.getId());
            busiL2VpnQoslevelMapper.deleteBusiL2VpnQoslevelByL2VpnId(busiL2Vpn.getId());
            i = busiL2VpnMapper.updateBusiL2Vpn(busiL2Vpn);
            insertBusiL2VpnNodeAttachInfo(busiL2Vpn);
        } else {
            busiL2Vpn.setCreateBy(SecurityUtils.getUsername());
            busiL2Vpn.setCreateTime(DateUtils.getNowDate());
            busiL2Vpn.setUpdateBy(SecurityUtils.getUsername());
            busiL2Vpn.setUpdateTime(DateUtils.getNowDate());
            i = busiL2VpnMapper.insertBusiL2Vpn(busiL2Vpn);
            //增加节点属性-节点信息、节点属性-业务接入接口参数
            insertBusiL2VpnNodeAttachInfo(busiL2Vpn);
        }


        // 写故障检测设置/OAM
        //一源两宿
        if ("一源两宿".equals(busiL2Vpn.getNetworkMode())) {
            BusiL2VpnNode vpnNode = new BusiL2VpnNode();
            vpnNode.setL2VpnId(busiL2Vpn.getId());
            List<BusiL2VpnNodeListVo> vpnNodeListVos = busiL2VpnNodeMapper.selectBusiL2VpnNodeList(vpnNode);
            BusiL2VpnPw pw = new BusiL2VpnPw();
            pw.setL2VpnId(busiL2Vpn.getId());
            List<BusiL2VpnPwListVo> pws = busiL2VpnPwMapper.selectBusiL2VpnPwList(pw);
            for (BusiL2VpnPwListVo busiL2VpnPw : pws) {
                BusiL2VpnAlarmoam busiL2VpnAlarmoam = new BusiL2VpnAlarmoam();
                busiL2VpnAlarmoam.setId(IdUtils.simpleUUID()); // id
                busiL2VpnAlarmoam.setElementId(busiL2VpnPw.getStartElementId()); // elementId
                busiL2VpnAlarmoam.setL2VpnId(busiL2VpnPw.getL2VpnId()); // l2vpnid
                String oamId = globalIncrementService.getNextOamIdIncrementNumber();
                busiL2VpnAlarmoam.setOamId(oamId);
                busiL2VpnAlarmoam.setMeName(oamId); // me-name
                busiL2VpnAlarmoam.setMegName("meg" + globalIncrementService.getNextPwClassNameIncrementNumber()); // oamtype
                busiL2VpnAlarmoam.setMeg("123456/789ABC/1/2/7");
                busiL2VpnAlarmoam.setRouteType(busiL2VpnPw.getRole());
                BusiL2VpnNodeListVo nodeListVo = vpnNodeListVos.stream().filter(n->busiL2VpnAlarmoam.getElementId().equals(n.getElementId()))
                        .findFirst().orElse(null);
                if (nodeListVo != null) {
                    busiL2VpnAlarmoam.setNodeType(nodeListVo.getNodeRole()); // 节点类型
                }
                l2VpnAlarmoamService.insertBusiL2VpnAlarmoam(busiL2VpnAlarmoam);

                busiL2VpnAlarmoam.setId(IdUtils.simpleUUID()); // id
                busiL2VpnAlarmoam.setElementId(busiL2VpnPw.getEndElementId());
                busiL2VpnAlarmoam.setL2VpnId(busiL2VpnPw.getL2VpnId()); // l2vpnid
                String oamId1 = globalIncrementService.getNextOamIdIncrementNumber();
                busiL2VpnAlarmoam.setOamId(oamId1);
                busiL2VpnAlarmoam.setMeName(oamId1);
                busiL2VpnAlarmoam.setMegName("meg" + globalIncrementService.getNextPwClassNameIncrementNumber());
                busiL2VpnAlarmoam.setMeg("123456/789ABC/2/1/7");
                busiL2VpnAlarmoam.setRouteType(busiL2VpnPw.getRole());
                nodeListVo = vpnNodeListVos.stream().filter(n->busiL2VpnAlarmoam.getElementId().equals(n.getElementId()))
                        .findFirst().orElse(null);
                if (nodeListVo != null) {
                    busiL2VpnAlarmoam.setNodeType(nodeListVo.getNodeRole()); // 节点类型
                }
                l2VpnAlarmoamService.insertBusiL2VpnAlarmoam(busiL2VpnAlarmoam);
            }
        }


        return i;
    }

    /**
     * 增加节点属性-节点信息、节点属性-业务接入接口参数
     * @param busiL2Vpn
     */
    private void insertBusiL2VpnNodeAttachInfo(BusiL2Vpn busiL2Vpn) {
        //生成节点属性-节点信息（与节点列表对应） 通过外键l2vpnId查询节点列表
        BusiL2VpnNode busiL2VpnNode = new BusiL2VpnNode();
        busiL2VpnNode.setL2VpnId(busiL2Vpn.getId());
        List<BusiL2VpnNodeListVo> busiL2VpnNodes = busiL2VpnNodeMapper.selectBusiL2VpnNodeList(busiL2VpnNode);
        for (BusiL2VpnNode node : busiL2VpnNodes) {
            BusiL2VpnNodeInfo nodeInfo = new BusiL2VpnNodeInfo();
            nodeInfo.setId(IdUtils.simpleUUID());
            nodeInfo.setL2VpnId(busiL2Vpn.getId());
            nodeInfo.setElementId(node.getElementId());
            nodeInfo.setCreateTime(DateUtils.getNowDate());
            nodeInfo.setUpdateTime(DateUtils.getNowDate());
            nodeInfo.setCreateBy(SecurityUtils.getUsername());
            nodeInfo.setUpdateBy(SecurityUtils.getUsername());
            // l2vpn业务，节点信息表数据自动生成，生成交叉方式：vpws;
            // VPWS业务名称:vpws_{d};
            // ARP双发选收使能:disable;
            // NDP缓存双发使能:disable
            nodeInfo.setBusiName("vpws_" + globalIncrementService.getNextVPWSIncrementNumber());
            nodeInfo.setCorssType("vpws");
            nodeInfo.setArpEnable("disable");
            nodeInfo.setNdpCacheEnable("disable");
            busiL2VpnNodeInfoMapper.insertBusiL2VpnNodeInfo(nodeInfo);
        }
        //生成节点属性-业务接入接口参数（与节点列表对应） 通过外键l2vpnId查询节点列表
        for (BusiL2VpnNode node : busiL2VpnNodes) {
            BusiL2VpnNodePort port = new BusiL2VpnNodePort();
            port.setId(IdUtils.simpleUUID());
            port.setL2VpnId(busiL2Vpn.getId());
            port.setElementId(node.getElementId());
            port.setCreateTime(DateUtils.getNowDate());
            port.setUpdateTime(DateUtils.getNowDate());
            port.setCreateBy(SecurityUtils.getUsername());
            port.setUpdateBy(SecurityUtils.getUsername());
            busiL2VpnNodePortMapper.insertBusiL2VpnNodePort(port);

            // 初始化业务设备激活信息
            BusiElementStatus busiElementStatus = new BusiElementStatus();
            busiElementStatus.setId(IdUtils.simpleUUID());
            busiElementStatus.setBusiType("L2VPN");
            busiElementStatus.setBusiId(busiL2Vpn.getId());
            busiElementStatus.setElementId(node.getElementId());
            busiElementStatus.setStatus("0");
            busiElementStatus.setCreateTime(DateUtils.getNowDate());
            busiElementStatus.setUpdateTime(DateUtils.getNowDate());
            busiElementStatus.setCreateBy(SecurityUtils.getUsername());
            busiElementStatus.setUpdateBy(SecurityUtils.getUsername());
            busiElementStatusMapper.insertBusiElementStatus(busiElementStatus);
        }
        //无保护、PW1+1保护  通过模版生成qos数据
//        if ("无保护".equals(busiL2Vpn.getProtectType())){
        // 一源一宿
        if ("一源一宿".equals(busiL2Vpn.getNetworkMode())) {
            //调用qos-flow模版数据并新增
            BusiL2VpnQosflow busiL2VpnQosflow = busiL2VpnQosflowMapper.selectBusiL2VpnQosflowTemplate();
            busiL2VpnQosflow.setId(IdUtils.simpleUUID());
            busiL2VpnQosflow.setL2VpnId(busiL2Vpn.getId());
            busiL2VpnQosflow.setRouteType("1");
            busiL2VpnQosflow.setUpdateBy(SecurityUtils.getUsername());
            busiL2VpnQosflow.setUpdateTime(DateUtils.getNowDate());
            busiL2VpnQosflow.setCreateBy(SecurityUtils.getUsername());
            busiL2VpnQosflow.setCreateTime(DateUtils.getNowDate());
            busiL2VpnQosflow.setTemplateFlag("N");
            busiL2VpnQosflowMapper.insertBusiL2VpnQosflow(busiL2VpnQosflow);
            //调用qos-level模版数据并新增
            BusiL2VpnQoslevel busiL2VpnQoslevel = busiL2VpnQoslevelMapper.selectBusiL2VpnQoslevelTemplate();
            busiL2VpnQoslevel.setId(IdUtils.simpleUUID());
            busiL2VpnQoslevel.setL2VpnId(busiL2Vpn.getId());
            busiL2VpnQoslevel.setRouteType("1");
            busiL2VpnQoslevel.setUpdateBy(SecurityUtils.getUsername());
            busiL2VpnQoslevel.setUpdateTime(DateUtils.getNowDate());
            busiL2VpnQoslevel.setCreateBy(SecurityUtils.getUsername());
            busiL2VpnQoslevel.setCreateTime(DateUtils.getNowDate());
            busiL2VpnQoslevel.setTemplateFlag("N");
            busiL2VpnQoslevelMapper.insertBusiL2VpnQoslevel(busiL2VpnQoslevel);
            //调用qos-lsp模版数据并新增
            BusiL2VpnQoslsp busiL2VpnQoslsp = busiL2VpnQoslspMapper.selectBusiL2VpnQoslspTemplate();
            busiL2VpnQoslsp.setId(IdUtils.simpleUUID());
            busiL2VpnQoslsp.setL2VpnId(busiL2Vpn.getId());
            busiL2VpnQoslsp.setRouteType("1");
            busiL2VpnQoslsp.setUpdateBy(SecurityUtils.getUsername());
            busiL2VpnQoslsp.setUpdateTime(DateUtils.getNowDate());
            busiL2VpnQoslsp.setCreateBy(SecurityUtils.getUsername());
            busiL2VpnQoslsp.setCreateTime(DateUtils.getNowDate());
            busiL2VpnQoslsp.setTemplateFlag("N");
            busiL2VpnQoslspMapper.insertBusiL2VpnQoslsp(busiL2VpnQoslsp);
        }
        //else if ("PW1+1保护".equals(busiL2Vpn.getProtectType())){
        else {
            //新增2条数据
            //调用qos-flow模版数据并新增
            BusiL2VpnQosflow busiL2VpnQosflow = busiL2VpnQosflowMapper.selectBusiL2VpnQosflowTemplate();
            busiL2VpnQosflow.setId(IdUtils.simpleUUID());
            busiL2VpnQosflow.setL2VpnId(busiL2Vpn.getId());
            busiL2VpnQosflow.setUpdateBy(SecurityUtils.getUsername());
            busiL2VpnQosflow.setUpdateTime(DateUtils.getNowDate());
            busiL2VpnQosflow.setCreateBy(SecurityUtils.getUsername());
            busiL2VpnQosflow.setCreateTime(DateUtils.getNowDate());
            busiL2VpnQosflow.setTemplateFlag("N");
            busiL2VpnQosflow.setRouteType("1");
            busiL2VpnQosflowMapper.insertBusiL2VpnQosflow(busiL2VpnQosflow);
            busiL2VpnQosflow.setId(IdUtils.simpleUUID());
            busiL2VpnQosflow.setRouteType("2");
            busiL2VpnQosflowMapper.insertBusiL2VpnQosflow(busiL2VpnQosflow);
            //调用qos-level模版数据并新增
            BusiL2VpnQoslevel busiL2VpnQoslevel = busiL2VpnQoslevelMapper.selectBusiL2VpnQoslevelTemplate();
            busiL2VpnQoslevel.setId(IdUtils.simpleUUID());
            busiL2VpnQoslevel.setL2VpnId(busiL2Vpn.getId());
            busiL2VpnQoslevel.setUpdateBy(SecurityUtils.getUsername());
            busiL2VpnQoslevel.setUpdateTime(DateUtils.getNowDate());
            busiL2VpnQoslevel.setCreateBy(SecurityUtils.getUsername());
            busiL2VpnQoslevel.setCreateTime(DateUtils.getNowDate());
            busiL2VpnQoslevel.setTemplateFlag("N");
            busiL2VpnQoslevel.setRouteType("1");
            busiL2VpnQoslevelMapper.insertBusiL2VpnQoslevel(busiL2VpnQoslevel);
            busiL2VpnQoslevel.setId(IdUtils.simpleUUID());
            busiL2VpnQoslevel.setRouteType("2");
            busiL2VpnQoslevelMapper.insertBusiL2VpnQoslevel(busiL2VpnQoslevel);

            //调用qos-lsp模版数据并新增
            BusiL2VpnQoslsp busiL2VpnQoslsp = busiL2VpnQoslspMapper.selectBusiL2VpnQoslspTemplate();
            busiL2VpnQoslsp.setId(IdUtils.simpleUUID());
            busiL2VpnQoslsp.setL2VpnId(busiL2Vpn.getId());
            busiL2VpnQoslsp.setUpdateBy(SecurityUtils.getUsername());
            busiL2VpnQoslsp.setUpdateTime(DateUtils.getNowDate());
            busiL2VpnQoslsp.setCreateBy(SecurityUtils.getUsername());
            busiL2VpnQoslsp.setCreateTime(DateUtils.getNowDate());
            busiL2VpnQoslsp.setTemplateFlag("N");
            busiL2VpnQoslsp.setRouteType("1");
            busiL2VpnQoslspMapper.insertBusiL2VpnQoslsp(busiL2VpnQoslsp);
            busiL2VpnQoslsp.setL2VpnId(busiL2Vpn.getId());
            busiL2VpnQoslsp.setId(IdUtils.simpleUUID());
            busiL2VpnQoslsp.setRouteType("2");
            busiL2VpnQoslspMapper.insertBusiL2VpnQoslsp(busiL2VpnQoslsp);
        }
    }

    /**
     * 修改业务-L2 VPN配置
     * 
     * @param busiL2Vpn 业务-L2 VPN配置
     * @return 结果
     */
    @Override
    public int updateBusiL2Vpn(BusiL2Vpn busiL2Vpn)
    {
        busiL2Vpn.setUpdateBy(SecurityUtils.getUsername());
        busiL2Vpn.setUpdateTime(DateUtils.getNowDate());
        return busiL2VpnMapper.updateBusiL2Vpn(busiL2Vpn);
    }

    /**
     * 批量删除业务-L2 VPN配置
     * 
     * @param ids 需要删除的业务-L2 VPN配置主键
     * @return 结果
     */
    @Override
    public int deleteBusiL2VpnByIds(String[] ids)
    {
        return busiL2VpnMapper.deleteBusiL2VpnByIds(ids);
    }

    /**
     * 删除业务-L2 VPN配置信息
     * 
     * @param id 业务-L2 VPN配置主键
     * @return 结果
     */
    @Override
    public int deleteBusiL2VpnById(String id)
    {
        return busiL2VpnMapper.deleteBusiL2VpnById(id);
    }

    /**
     * 查询网元-业务-eline列表
     *
     * @param busiL2Vpn
     * @return
     */
    @Override
    public List<BusiL2VpnEleListVo> selectBusiL2VpnEleList(BusiL2VpnEleListVo busiL2Vpn) {
        return busiL2VpnMapper.selectBusiL2VpnEleList(busiL2Vpn);
    }
}
